//
//  NRWCarShareTool.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarShareTool.h"
#import <WXApi.h>
#import "NRWCarDetailModel.h"

@implementation NRWCarShareTool

#pragma mark - 分享车辆详情, title = carTitle
+ (void)shareCarDetailWithCarDetailModel:(NRWCarDetailModel *)model shareType:(ShareType)type {
    
    if (type == ShareTypeWeiXin || type == ShareTypeWXCircle) {
        
        NSString *carMileage = [self formatCarMileage:[NSNumber numberWithFloat:model.result.carMileage]];
        NSString *carLicenceDate = [self formatCarLicenceDate:model.result.carLicenceDate carLicenceState:model.result.carLicenceState];
        NSString *imageUrl = [NSString stringWithFormat:@"%@%@", imageApi(), model.result.carImageUrl];
        NSString *webpageUrl = [self formatCarDetailUrlWithMarketId:model.result.marketId shopId:model.result.shopId carId:model.result.carId model:model];
        UIImage *image = [self imageWithImageSimple:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imageUrl]]]] scaledToSize:CGSizeMake(100, 100)];
        if (type == ShareTypeWeiXin) {
            [self wxShareWithTitle:model.result.carTitle description:[NSString stringWithFormat:@"%@\n%@", carMileage, carLicenceDate] thumbImageURL:image webpageUrl:webpageUrl type:WXSceneSession];
        } else {
            [self wxShareWithTitle:model.result.carTitle description:[NSString stringWithFormat:@"%@\n%@", carMileage, carLicenceDate] thumbImageURL:image webpageUrl:webpageUrl type:WXSceneTimeline];
        }
    }
}

#pragma mark - 分享车辆详情, title 自定义
+ (void)shareCarDetailWithCarDetailModelCustomerTitle:(NSString *)title url:(NSString *)url shareType:(ShareType)type{

    int wxType = 0;
    if (type == ShareTypeWeiXin) {
        wxType = WXSceneSession;
    } else {
        wxType = WXSceneTimeline;
    }

    if (type == ShareTypeWeiXin || type == ShareTypeWXCircle) {
        [self wxShareWithTitle:title description:nil thumbImageURL:[UIImage imageNamed:@"mine_header_normal"] webpageUrl:url type:wxType];
    } else if (type == ShareTypeQQ || type == ShareTypeQQCircle) {

    }
}


#pragma mark - 分享链接整理数据
+ (NSString *)formatCarMileage:(NSNumber *)carMileage {
    
    if (carMileage.floatValue < 0.1 && carMileage.floatValue >= 0) {
        return [NSString stringWithFormat:@"行驶里程:%d公里", (int)(carMileage.floatValue * 1000)];
    } else {
        return [NSString stringWithFormat:@"行驶里程:%.2f万公里", carMileage.floatValue];
    }
}

+ (NSString *)formatCarLicenceDate:(NSInteger)carLicenceDate carLicenceState:(NSInteger)carLicenceState {
    
    if (carLicenceState == 1) {
        return [NSString stringWithFormat:@"上牌时间:%@", [NRWCarTurnTime timestampSwitchTime:carLicenceDate andFormatter:@"yyyy-MM"]];
    } else if (carLicenceState == 2) {
        return @"上牌时间: 未上牌";
    } else if (carLicenceState == 3) {
        return @"上牌时间: 新车";
    }
    return nil;
}

+ (NSString *)formatCarDetailUrlWithMarketId:(NSString *)marketId shopId:(NSString *)shopId carId:(NSString *)carId model:(NRWCarDetailModel *)model {
    
    return [NSString stringWithFormat:@"%@/wx/index.html#/detail/%@", Api(), model.result.carId];
}

#pragma mark - 分享图片
+ (void)shareImage:(UIImage *)image WithShareType:(ShareType)type {
    
    if (type == ShareTypeWeiXin) {
        [self wxShareImage:image type:WXSceneSession];
    } else {
        [self wxShareImage:image type:WXSceneTimeline];
    }
    
}

#pragma mark - 微信分享链接 公用方法
+ (void)wxShareWithTitle:(NSString *)title description:(NSString *)description thumbImageURL:(UIImage *)thumbImage webpageUrl:(NSString *)webpageUrl type:(int)type {
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = [title stringByAppendingString:@"\n"];
    message.description = description;
    [message setThumbImage:thumbImage];
        
    WXWebpageObject *webpage = [WXWebpageObject object];
    webpage.webpageUrl = webpageUrl;
    message.mediaObject = webpage;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;
    [WXApi sendReq:req];
}

#pragma mark - 分享图片 公用方法
+ (void)wxShareImage:(UIImage *)image type:(int)type {
    
    WXMediaMessage *message = [WXMediaMessage message];
    
    UIImage *thumbImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(image.size.width / 2, image.size.height / 2)];
    
    [message setThumbImage:thumbImage];
    
    WXImageObject *imageObj = [WXImageObject object];
    imageObj.imageData = UIImagePNGRepresentation(image);
    
    message.mediaObject = imageObj;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;
    [WXApi sendReq:req];
}

#pragma mark - 压缩图片(重绘图片大小)
+ (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize {
    
    //首先根据image的size的长宽比和newSize进行设置新图片的大小（为了达到等比例缩放不变形的目的）
    CGFloat wTmp;
    CGFloat hTmp;
    CGSize imgSize = image.size;
    if (imgSize.width > imgSize.height) {
        wTmp=newSize.width;
        hTmp = imgSize.height * wTmp / imgSize.width;
    } else {
        hTmp=newSize.height;
        wTmp = imgSize.width * hTmp / imgSize.height;
    }
    
    // Create a graphics image context
    UIGraphicsBeginImageContext(CGSizeMake(wTmp, hTmp));
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,wTmp,hTmp)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return [UIImage imageWithData:UIImageJPEGRepresentation(newImage, 0.5)];
}


+ (void)shareCarDetailWithCarDetailUrl:(NSString *)Url imageUrl:(NSString *)imageUrl customerTitle:(NSString *)title shopName:(NSString *)shopName shareType:(ShareType)type{
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    [message setThumbImage:[self imageWithImageSimple:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", imageApi(),imageUrl]]]] scaledToSize:CGSizeMake(100, 100)]];
    message.description = shopName;
    WXWebpageObject *webpage = [WXWebpageObject object];
    webpage.webpageUrl = Url;
    message.mediaObject = webpage;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;
    
    [WXApi sendReq:req];
}

@end
